home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / gtlayout / source / ltp_createmenutaglist.c < prev    next >
C/C++ Source or Header  |  1999-04-19  |  10KB  |  512 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1998 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. /*****************************************************************************/
  15.  
  16. #include <dos/dos.h>    /* For AmigaDOS error definitions */
  17.  
  18. /*****************************************************************************/
  19.  
  20. #include "Assert.h"
  21.  
  22. /*****************************************************************************/
  23.  
  24. #ifdef DO_MENUS    /* Support code */
  25.  
  26.     /* LTP_CreateMenuTagList(RootMenu *Root,LONG *Error,struct TagItem *TagList):
  27.      *
  28.      *    Create the menu strip data.
  29.      */
  30.  
  31. BOOL
  32. LTP_CreateMenuTagList(RootMenu *Root,LONG *ErrorPtr,struct TagItem *TagList)
  33. {
  34.     MenuNode        *LastMenu    = NULL;
  35.     ItemNode        *LastItem    = NULL;
  36.     ItemNode        *LastSub    = NULL;
  37.  
  38.     MenuNode        *Menu;
  39.     ItemNode        *Item;
  40.     ItemNode        *Sub;
  41.  
  42.     struct TagItem    *List = TagList,*Entry;
  43.  
  44.     struct NewMenu     Template;
  45.  
  46.     LONG             Char        = 0;
  47.     LONG             Code        = 0;
  48.     ULONG             Qualifier    = 0;
  49.     ULONG             ID            = 0;
  50.  
  51.     LONG             Error        = 0;
  52.  
  53.     BOOL             Starting    = TRUE;
  54.  
  55.     memset(&Template,0,sizeof(Template));    /* For the sake of the compiler, make sure this is initialized */
  56.  
  57.     for(;;)
  58.     {
  59.         Entry = NextTagItem(&List);
  60.  
  61.         if(!Entry || (Entry && Entry->ti_Tag >= LAMN_TitleText && Entry->ti_Tag <= LAMN_SubID))
  62.         {
  63.             if(Starting)
  64.                 Starting = FALSE;
  65.             else
  66.             {
  67.                     // Check the type
  68.  
  69.                 switch(Template.nm_Type)
  70.                 {
  71.                         // Found a new menu?
  72.  
  73.                     case NM_TITLE:
  74.  
  75.                         DB(kprintf("NM_TITLE |%s|\n",Template.nm_Label));
  76.  
  77.                             // Stop on weird labels
  78.  
  79.                         if(Template.nm_Label == NM_BARLABEL)
  80.                             Error = ERROR_OBJECT_WRONG_TYPE;
  81.                         else
  82.                         {
  83.                                 // Choose the menu to use
  84.  
  85.                             if(!LastMenu)
  86.                                 Menu = (MenuNode *)&Root->Node;
  87.                             else
  88.                                 Menu = NULL;
  89.  
  90.                             DB(kprintf("         make menu\n"));
  91.  
  92.                                 // Make room for the menu
  93.  
  94.                             if(Menu = LTP_MakeMenu(Root,Menu,&Template))
  95.                             {
  96.                                 Menu->ID = ID;
  97.  
  98.                                 ID = 0;
  99.  
  100.                                     // Link to last menu
  101.  
  102.                                 if(LastMenu)
  103.                                     LastMenu->Menu.NextMenu = &Menu->Menu;
  104.  
  105.                                     // Mark the last menu item
  106.  
  107.                                 if(LastSub)
  108.                                     LastSub->Flags |= ITEMF_LastItem;
  109.                                 else
  110.                                 {
  111.                                     if(LastItem)
  112.                                         LastItem->Flags |= ITEMF_LastItem;
  113.                                 }
  114.  
  115.                                 LastMenu    = Menu;
  116.                                 LastItem    = NULL;
  117.                                 LastSub        = NULL;
  118.  
  119.                                     // Add the menu to the list
  120.  
  121.                                 AddTail((struct List *)&Root->MenuList,(struct Node *)Menu);
  122.                             }
  123.                             else
  124.                                 Error = ERROR_NO_FREE_STORE;
  125.                         }
  126.  
  127.                         break;
  128.  
  129.                         // Found a new menu item?
  130.  
  131.                     case NM_ITEM:
  132.  
  133.                         DB(kprintf("NM_ITEM |%s|\n",Template.nm_Label == NM_BARLABEL ? (STRPTR)"«Bar»" : Template.nm_Label));
  134.  
  135.                             // We need a menu to attach this to
  136.  
  137.                         if(!LastMenu)
  138.                             Error = ERROR_INVALID_COMPONENT_NAME;
  139.                         else
  140.                         {
  141.                             DB(kprintf("        make item\n"));
  142.  
  143.                                 // Make room for the item
  144.  
  145.                             if(Item = LTP_MakeItem(Root,&Template))
  146.                             {
  147.                                     // Put the char/code/qualifier in
  148.  
  149.                                 Item->Char        = Char;
  150.                                 Item->Code        = Code;
  151.                                 Item->Qualifier    = Qualifier;
  152.                                 Item->ID        = ID;
  153.  
  154.                                 ID = 0;
  155.  
  156.                                     // Link it in
  157.  
  158.                                 if(LastItem)
  159.                                     LastItem->Item.NextItem = &Item->Item;
  160.                                 else
  161.                                     LastMenu->Menu.FirstItem = &Item->Item;
  162.  
  163.                                 LastItem    = Item;
  164.                                 LastSub        = NULL;
  165.  
  166.                                     // Add it to the item list
  167.  
  168.                                 AddTail((struct List *)&Root->ItemList,(struct Node *)Item);
  169.                             }
  170.                             else
  171.                                 Error = ERROR_NO_FREE_STORE;
  172.                         }
  173.  
  174.                         break;
  175.  
  176.                     case NM_SUB:
  177.  
  178.                         DB(kprintf("NM_SUB |%s|\n",Template.nm_Label == NM_BARLABEL ? (STRPTR)"«Bar»" : Template.nm_Label));
  179.  
  180.                             // We need a menu item to attach this to
  181.  
  182.                         if(!LastItem)
  183.                             Error = ERROR_INVALID_COMPONENT_NAME;
  184.                         else
  185.                         {
  186.                             DB(kprintf("       make sub\n"));
  187.  
  188.                                 // Make room for the item
  189.  
  190.                             if(Sub = LTP_MakeItem(Root,&Template))
  191.                             {
  192.                                     // Put the char/code/qualifier in
  193.  
  194.                                 Sub->Char        = Char;
  195.                                 Sub->Code        = Code;
  196.                                 Sub->Qualifier    = Qualifier;
  197.                                 Sub->ID            = ID;
  198.  
  199.                                 ID = 0;
  200.  
  201.                                     // Make sure it stays a submenu item
  202.  
  203.                                 Sub->Flags |= ITEMF_IsSub;
  204.  
  205.                                     // Link it in
  206.  
  207.                                 if(LastSub)
  208.                                     LastSub->Item.NextItem = &Sub->Item;
  209.                                 else
  210.                                 {
  211.                                         // We cannot attach submenu items to
  212.                                         // separator bars
  213.  
  214.                                     if(LastItem->Flags & ITEMF_IsBar)
  215.                                         Error = ERROR_INVALID_COMPONENT_NAME;
  216.                                     else
  217.                                     {
  218.                                         DB(kprintf("-----------> |%s| has submenu items\n",((struct IntuiText *)LastItem->Item.ItemFill)->IText));
  219.  
  220.                                             // Link the submenu item in
  221.  
  222.                                         LastItem->Item.SubItem = &Sub->Item;
  223.  
  224.                                             // Zap the command sequence data
  225.  
  226.                                         LastItem->Flags        = (LastItem->Flags & ~ITEMF_Command) | ITEMF_HasSub;
  227.                                         LastItem->ExtraLabel    = NULL;
  228.  
  229.                                         LastItem->Item.Flags    &= ~COMMSEQ;
  230.                                         LastItem->Item.Command     = 0;
  231.  
  232.                                             // This is the first submenu item for this menu item
  233.  
  234.                                         Sub->Flags |= ITEMF_FirstSub;
  235.                                     }
  236.                                 }
  237.  
  238.                                 LastSub = Sub;
  239.  
  240.                                     // Add it to the list
  241.  
  242.                                 AddTail((struct List *)&Root->ItemList,(struct Node *)Sub);
  243.                             }
  244.                             else
  245.                                 Error = ERROR_NO_FREE_STORE;
  246.                         }
  247.  
  248.                         break;
  249.                 }
  250.  
  251.                 memset(&Template,0,sizeof(Template));
  252.             }
  253.         }
  254.  
  255.         if(Entry)
  256.         {
  257.             switch(Entry->ti_Tag)
  258.             {
  259.                 case LAMN_TitleText:
  260.  
  261.                     Template.nm_Type    = NM_TITLE;
  262.                     Template.nm_Label    = (STRPTR)Entry->ti_Data;
  263.  
  264.                     break;
  265.  
  266.                 case LAMN_TitleID:
  267.  
  268.                     if(!Root->LocaleHook)
  269.                     {
  270.                         Error = ERROR_OBJECT_NOT_FOUND;
  271.                         break;
  272.                     }
  273.  
  274.                     Template.nm_Type    = NM_TITLE;
  275.                     Template.nm_Label    = (STRPTR)CallHookPkt(Root->LocaleHook,Root->Handle,(APTR)Entry->ti_Data);
  276.                     break;
  277.  
  278.                 case LAMN_ItemText:
  279.  
  280.                     Template.nm_Type    = NM_ITEM;
  281.                     Template.nm_Label    = (STRPTR)Entry->ti_Data;
  282.  
  283.                     if(Template.nm_Label != NM_BARLABEL && !Template.nm_Label[1])
  284.                     {
  285.                         if(Template.nm_Label[0])
  286.                             Template.nm_CommKey = Template.nm_Label;
  287.  
  288.                         Template.nm_Label = &Template.nm_Label[2];
  289.                     }
  290.  
  291.                     break;
  292.  
  293.                 case LAMN_ItemID:
  294.  
  295.                     if(!Root->LocaleHook)
  296.                     {
  297.                         Error = ERROR_OBJECT_NOT_FOUND;
  298.                         break;
  299.                     }
  300.  
  301.                     Template.nm_Type    = NM_ITEM;
  302.                     Template.nm_Label    = (STRPTR)CallHookPkt(Root->LocaleHook,Root->Handle,(APTR)Entry->ti_Data);
  303.  
  304.                     if(Template.nm_Label != NM_BARLABEL && !Template.nm_Label[1])
  305.                     {
  306.                         if(Template.nm_Label[0])
  307.                             Template.nm_CommKey = Template.nm_Label;
  308.  
  309.                         Template.nm_Label = &Template.nm_Label[2];
  310.                     }
  311.  
  312.                     break;
  313.  
  314.                 case LAMN_SubText:
  315.  
  316.                     Template.nm_Type    = NM_SUB;
  317.                     Template.nm_Label    = (STRPTR)Entry->ti_Data;
  318.  
  319.                     if(Template.nm_Label != NM_BARLABEL && !Template.nm_Label[1])
  320.                     {
  321.                         if(Template.nm_Label[0])
  322.                             Template.nm_CommKey = Template.nm_Label;
  323.  
  324.                         Template.nm_Label = &Template.nm_Label[2];
  325.                     }
  326.  
  327.                     break;
  328.  
  329.                 case LAMN_SubID:
  330.  
  331.                     if(!Root->LocaleHook)
  332.                     {
  333.                         Error = ERROR_OBJECT_NOT_FOUND;
  334.                         break;
  335.                     }
  336.  
  337.                     Template.nm_Type    = NM_SUB;
  338.                     Template.nm_Label    = (STRPTR)CallHookPkt(Root->LocaleHook,Root->Handle,(APTR)Entry->ti_Data);
  339.  
  340.                     if(Template.nm_Label != NM_BARLABEL && !Template.nm_Label[1])
  341.                     {
  342.                         if(Template.nm_Label[0])
  343.                             Template.nm_CommKey = Template.nm_Label;
  344.  
  345.                         Template.nm_Label = &Template.nm_Label[2];
  346.                     }
  347.  
  348.                     break;
  349.  
  350.                 case LAMN_KeyText:
  351.  
  352.                     Template.nm_CommKey = (STRPTR)Entry->ti_Data;
  353.                     break;
  354.  
  355.                 case LAMN_KeyID:
  356.  
  357.                     if(!Root->LocaleHook)
  358.                     {
  359.                         Error = ERROR_OBJECT_NOT_FOUND;
  360.                         break;
  361.                     }
  362.  
  363.                     Template.nm_CommKey = (STRPTR)CallHookPkt(Root->LocaleHook,Root->Handle,(APTR)Entry->ti_Data);
  364.                     break;
  365.  
  366.                 case LAMN_CommandText:
  367.  
  368.                     Template.nm_CommKey    = (STRPTR)Entry->ti_Data;
  369.                     Template.nm_Flags    = Template.nm_Flags | NM_COMMANDSTRING;
  370.                     break;
  371.  
  372.                 case LAMN_CommandID:
  373.  
  374.                     if(!Root->LocaleHook)
  375.                     {
  376.                         Error = ERROR_OBJECT_NOT_FOUND;
  377.                         break;
  378.                     }
  379.  
  380.                     Template.nm_CommKey    = (STRPTR)CallHookPkt(Root->LocaleHook,Root->Handle,(APTR)Entry->ti_Data);
  381.                     Template.nm_Flags    = Template.nm_Flags | NM_COMMANDSTRING;
  382.                     break;
  383.  
  384.                 case LAMN_MutualExclude:
  385.  
  386.                     Template.nm_MutualExclude = (ULONG)Entry->ti_Data;
  387.                     break;
  388.  
  389.                 case LAMN_UserData:
  390.  
  391.                     Template.nm_UserData = (APTR)Entry->ti_Data;
  392.                     break;
  393.  
  394.                 case LAMN_Disabled:
  395.  
  396.                     if(Entry->ti_Data)
  397.                     {
  398.                         if(Template.nm_Type == NM_TITLE)
  399.                             Template.nm_Flags |= NM_MENUDISABLED;
  400.                         else
  401.                             Template.nm_Flags |= NM_ITEMDISABLED;
  402.                     }
  403.                     else
  404.                     {
  405.                         if(Template.nm_Type == NM_TITLE)
  406.                             Template.nm_Flags &= ~NM_MENUDISABLED;
  407.                         else
  408.                             Template.nm_Flags &= ~NM_ITEMDISABLED;
  409.                     }
  410.  
  411.                     break;
  412.  
  413.                 case LAMN_CheckIt:
  414.  
  415.                     if(Template.nm_Type != NM_TITLE)
  416.                     {
  417.                         if(Entry->ti_Data)
  418.                             Template.nm_Flags |= CHECKIT;
  419.                         else
  420.                             Template.nm_Flags &= ~CHECKIT;
  421.                     }
  422.  
  423.                     break;
  424.  
  425.                 case LAMN_Checked:
  426.  
  427.                     if(Template.nm_Type != NM_TITLE)
  428.                     {
  429.                         if(Entry->ti_Data)
  430.                             Template.nm_Flags |= CHECKIT | CHECKED;
  431.                         else
  432.                             Template.nm_Flags &= ~CHECKED;
  433.                     }
  434.  
  435.                     break;
  436.  
  437.                 case LAMN_Toggle:
  438.  
  439.                     if(Template.nm_Type != NM_TITLE)
  440.                     {
  441.                         if(Entry->ti_Data)
  442.                             Template.nm_Flags |= CHECKIT | MENUTOGGLE;
  443.                         else
  444.                             Template.nm_Flags &= ~MENUTOGGLE;
  445.                     }
  446.  
  447.                     break;
  448.  
  449.                 case LAMN_Code:
  450.  
  451.                     Code = Entry->ti_Data;
  452.                     Char = 0;
  453.  
  454.                     break;
  455.  
  456.                 case LAMN_Qualifier:
  457.  
  458.                     Qualifier = Entry->ti_Data;
  459.                     break;
  460.  
  461.                 case LAMN_Char:
  462.  
  463.                     Char = Entry->ti_Data;
  464.                     Code = 0;
  465.  
  466.                     break;
  467.  
  468.                 case LAMN_ID:
  469.  
  470.                     ID = (ULONG)Entry->ti_Data;
  471.                     break;
  472.             }
  473.         }
  474.         else
  475.         {
  476.                 // Mark the last menu item
  477.  
  478.             if(LastSub)
  479.                 LastSub->Flags |= ITEMF_LastItem;
  480.             else
  481.             {
  482.                 if(LastItem)
  483.                     LastItem->Flags |= ITEMF_LastItem;
  484.             }
  485.  
  486.             break;
  487.         }
  488.     }
  489.  
  490.         // Did we create anything at all?
  491.  
  492.     if(!Error)
  493.     {
  494.         if(!Root->MenuList.mlh_Head->mln_Succ)
  495.             Error = ERROR_OBJECT_NOT_FOUND;
  496.         else
  497.             LTP_FixExtraLabel(Root,&Error);
  498.     }
  499.  
  500.     if(ErrorPtr)
  501.         *ErrorPtr = Error;
  502.  
  503.     DB(kprintf("through, error=%ld\n",Error));
  504.  
  505.     if(Error)
  506.         return(FALSE);
  507.     else
  508.         return(TRUE);
  509. }
  510.  
  511. #endif    /* DO_MENUS */
  512.